#include "DialogPen.h"
#include "ui_DialogPen.h"
#include <QColorDialog>
#include <QDebug>

DialogPen::DialogPen(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogPen)
{
    ui->setupUi(this);

    ui->BtnColor->setAutoFillBackground(true); // 设置颜色按钮的背景色

    QString colorStr = "background-color:rgb(255,85,0);";
    ui->BtnColor->setStyleSheet(colorStr);

    //追加画笔样式选择
    QStringList penStyle;
    penStyle<<"NoPen"<<"SolidLine"<<"DashLine"<<"DotLine"<<"DashDotLine"
<<"DashDotDotLine"<<"CustomDashLine";
    ui->comboBoxPenStyle->addItems(penStyle);
}

DialogPen::~DialogPen()
{
    delete ui;
}

void DialogPen::setPen(const QPen &pen)
{
    //设置QPen，并刷新显示界面
    m_pen=pen;

    ui->spinBoxLineWidth->setValue(pen.width()); //线宽
    int i=static_cast<int>(pen.style()); //枚举 类型转换为整型

    ui->comboBoxPenStyle->setCurrentIndex(i) ;
    QColor color=pen.color() ;

    QString colorStr=QString("background-color:rgb(%1,%2,%3);").arg(color.red()).arg(color.green()).arg(color.blue());
    ui->BtnColor->setStyleSheet(colorStr);

}

QPen DialogPen::getPen()
{
    //获得设置的属性
    m_pen.setStyle(Qt::PenStyle(ui->comboBoxPenStyle->currentIndex())); //线型
    m_pen.setWidth(ui->spinBoxLineWidth->value()); //线宽
    QColor color=ui->BtnColor->palette().color(QPalette::Button);
    m_pen.setColor(color); //颜色.

    return m_pen;
}

QPen DialogPen::getPen(QPen iniPen, bool &ok)
{
    //静态函数，获取QPen
    DialogPen *Dlg=new DialogPen; //创建一个对话框

    Dlg->setPen(iniPen); //设置初始化QPen

    QPen pen;
    int ret=Dlg->exec(); //弹出对话框
    if (ret==QDialog::Accepted)
    {
        pen=Dlg->getPen(); //获取
        ok=true;
    }
    else
    {
        pen = iniPen;
        ok = false;
    }

    delete  Dlg;
    return pen;
}

void DialogPen::on_BtnColor_clicked()
{
    if(!mColorDialog){
        mColorDialog = new QColorDialog(this);
        connect(mColorDialog, &QColorDialog::colorSelected,this, &DialogPen::onColorSelectFinish);
    }
    ui->BtnColor->palette();

    mColorDialog->exec();
}

void DialogPen::onColorSelectFinish(QColor color)
{
    QString colorStr=QString("background-color:rgb(%1,%2,%3);").arg(color.red()).arg(color.green()).arg(color.blue());

    qInfo()<<"colorStr"<<colorStr;
    ui->BtnColor->setStyleSheet(colorStr);
}


void DialogPen::on_BtnOK_clicked()
{
    this->accept();
}


void DialogPen::on_BtnCancel_clicked()
{
    this->done(QDialog::Rejected);
}



QPen getPen(QPen iniPen, bool &ok)
{
    return DialogPen::getPen(iniPen, ok);
}
